package senior.MapTest.exer2;

import java.util.*;

/**
 * 用泛型重写：把中文和其对应的英文存入Map（中文作为键值）,实现翻译的功能 ：
 * 输入中文，获得对应的英文，不存在给出提示；
 *  输入英文，找到对应的中文，不存在给出提示。
 * @author shkstart
 * @creat 2022-04-14:40
 */
public class DictionaryTest {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("爸爸","Dad");
        map.put("东西好","well");
        map.put("身体好","well");
        map.put("妈妈","Mom");
        map.put("哥哥","brother");
        map.put("姐姐","sister");
        map.put("柯基","Corgi");
        map.put("布偶猫","Ragdoll");
        Set<Map.Entry<String, String>> set = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = set.iterator();
        Scanner sc = new Scanner(System.in);
        System.out.println("输入:");
        String s = sc.next();

        if(s.matches("[a-zA-Z]+") && !map.containsValue(s)){
            System.out.println("找不到此翻译");
            return;
        }
        if(s.matches("[\u4e00-\u9fa5]+") && !map.containsKey(s)){
            System.out.println("字典尚未收录");
            return;
        }
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            String key = next.getKey();
            String value = next.getValue();
            if(s.equals(key)){
                System.out.println("值：" + value);

            }else if(s.equals(value)){
                System.out.println("键：" + key);
            }
        }



    }
}
